package com.feng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.domain.ResponseResult;
import com.feng.domain.entity.Menu;
import com.feng.domain.vo.MenuTreeVo;
import com.feng.domain.vo.RoleMenuTreeSelectVo;
import com.feng.mapper.MenuMapper;
import com.feng.service.MenuService;
import com.feng.util.SystemConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuMapper menuMapper;
    @GetMapping("/list")
    public ResponseResult getMenuList(String status, String menuName){
        return menuService.getMenuList(status,menuName);
    }
    @PostMapping
    public ResponseResult add(@RequestBody Menu menu)
    {
        menuService.save(menu);
        return ResponseResult.okResult();
    }
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable Long id)
    {
        return ResponseResult.okResult(menuService.getById(id));
    }
    //修改菜单
    @PutMapping
    public ResponseResult edit(@RequestBody Menu menu) {
        if (menu.getId().equals(menu.getParentId())) {
            return ResponseResult.errorResult(500,"修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menuService.updateById(menu);
        return ResponseResult.okResult();
    }
    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public ResponseResult remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChild(menuId)) {
            return ResponseResult.errorResult(500,"存在子菜单不允许删除");
        }
        menuService.removeById(menuId);
        return ResponseResult.okResult();
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public ResponseResult treeselect() {
        //先将Menus集合全部查出来
        List<Menu> menus = menuService.list();
        //以树形结构将Menus查询出来
        List<MenuTreeVo> options =  SystemConverter.buildMenuSelectTree(menus);
        return ResponseResult.okResult(options);
        //
    }
    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeSelect(@PathVariable("id") Long id){
        //先将Menus集合全部查出来
        List<Menu> menus = menuService.list();
        //以树形结构将Menus查询出来
        List<MenuTreeVo> options =  SystemConverter.buildMenuSelectTree(menus);
        //判断是否为超级管理员
        if(id==1L){
            LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
            //条件查询只查询id
            queryWrapper.select(Menu::getId);
            List<Menu> list = menuService.list(queryWrapper);
            List<Long> collect = list.stream().map(e -> e.getId()).collect(Collectors.toList());
            RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(collect,options);
            return ResponseResult.okResult(vo);

        }
        List<Long> longs = menuMapper.selectMenuListByRoleId(id);
        RoleMenuTreeSelectVo vo = new RoleMenuTreeSelectVo(longs,options);
        return ResponseResult.okResult(vo);
    }

}
